﻿using BUGCOME.Infrastructure.Attribute;
using BUGCOME.Model.Auth;
using BUGCOME.Service.Auth.IServices;


/// <summary>
/// 团队成员服务实现类
/// 继承自基础服务类BaseService，并实现ITeamMemberService接口，处理团队成员相关业务逻辑
/// </summary>
[AppService(ServiceType = typeof(ITeamMemberService), ServiceLifetime = LifeTime.Transient)]
public class TeamMemberService : BaseService<TeamMember>, ITeamMemberService
{
    /// <summary>
    /// 异步添加团队成员信息
    /// </summary>
    /// <param name="teamMember">待添加的团队成员信息实体</param>
    /// <returns>添加成功后的数据实体（包含数据库自动生成的字段，如ID等）</returns>
    /// <exception cref="Exception">当用户已在该团队中时抛出异常</exception>
    public async Task<TeamMember> AddTeamMemberAsync(TeamMember teamMember)
    {
        // 检查该用户是否已加入当前团队（通过UserId和TeamId联合判断，确保用户在同一团队中不重复）
        bool isExist = await Queryable()
            .AnyAsync(m => m.IndividualUserId == teamMember.IndividualUserId && m.TeamUserId == teamMember.TeamUserId);

        // 若已存在则抛出异常，防止重复添加团队成员
        if (isExist)
        {
            throw new Exception("该用户已加入此团队，无法重复添加！");
        }

        // 将团队成员信息插入数据库，并返回插入后的实体（包含数据库生成的主键等信息）
        return await Insertable(teamMember).ExecuteReturnEntityAsync();
    }
}